/***************************************************
 ** @Desc : This file for 列表、增加、删除、更新
 ** @Time : 2018.12.27 16:38
 ** @Author : Joker
 ** @File : index.js
 ** @Last Modified by : Joker
 ** @Last Modified time: 2018.12.27 16:38
 ** @Software: GoLand
 ****************************************************/

var operator = {
    add: function () {
        x_admin_show("增加分账", '/index/add_ui.py/', 55, 68);
    },
    modofy: function (editId) {
        x_admin_show("修改分账", '/index/modify_ui.py/' + editId, 80, 98);
    },
    //删除
    del_config: function (params) {
        var msg = "确认要删除吗? 删除后不能恢复哟! ";
        layer.confirm(msg, {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.ajax({
                type: 'GET',
                url: "/index/delete.py/" + params,
                dataType: 'JSON',
                success: function (res) {
                    X_operator.common_return_page_index(res.code, res.msg);
                },
                error: function (XMLHttpRequest) {
                    layer.msg("触发异常! " + XMLHttpRequest.status, {
                        icon: 2,
                        shade: 0.3,
                        time: 2000
                    });
                }
            });
        });
    },
};

//监听提交
layui.use('form', function () {
    var form = layui.form;
    //修改
    form.on('submit(modify_config)', function () {
        $.ajax({
            type: 'POST',
            url: "/index/modify.py/",
            data: $("form").serialize(),
            dataType: 'JSON',
            success: function (res) {
                X_operator.common_return_page(res.code, res.msg)
            },
            error: function (XMLHttpRequest) {
                layer.msg("触发异常! " + XMLHttpRequest.status, {
                    icon: 2,
                    shade: 0.3,
                    time: 2000
                });
            }
        });
        return false;  //阻止表单跳转。
    });

    //增加
    form.on('submit(add_config)', function () {
        if ($("#trans_out").val() === $("#trans_in").val()) {
            layer.msg("分账PID与收账PID不能相同! ", {
                icon: 2,
                shade: 0.3,
                time: 2000
            });
        } else {
            $.ajax({
                type: 'POST',
                url: "/index/add.py/",
                data: $("form").serialize(),
                dataType: 'JSON',
                success: function (res) {
                    X_operator.common_return_page(res.code, res.msg, 'add')
                },
                error: function (XMLHttpRequest) {
                    layer.msg("触发异常! " + XMLHttpRequest.status, {
                        icon: 2,
                        shade: 0.3,
                        time: 2000
                    });
                }
            });
        }
        return false;  //阻止表单跳转。
    });
    //登录
    form.on('submit(login)', function () {
        $.ajax({
            type: "GET",
            url: "/verifyCaptcha.py/" + $("#captchaCode").val() + "/" + $("#captchaId").val(),
            success: function (res) {
                if (res.code === -9) {
                    //验证码不正确,刷新验证码
                    layer.msg(res.msg, {icon: 2, time: 2000, shade: 0.3}, function () {
                        login.flushCaptcha();
                    });
                } else {
                    //登录
                    $.ajax({
                        type: 'POST',
                        url: "/login.py/",
                        data: $("form").serialize(),
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.code == "-9") {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    shade: 0.3,
                                    time: 2000
                                });
                            } else {
                                window.location.href = res.url;
                            }
                        },
                        error: function (XMLHttpRequest) {
                            layer.msg("触发异常! " + XMLHttpRequest.status, {
                                icon: 2,
                                shade: 0.3,
                                time: 2000
                            });
                        }
                    });
                }
            }
        });
        return false;  //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});

//验证码
var login = {
    //点击刷新验证码
    changeImg: function () {
        login.setImgSrc($("#rcCaptcha-img"), "reload=" + (new Date()).getDate());
    },
    setImgSrc: function (obj, reload) {
        var $src = obj[0].src;
        var $flag = $src.indexOf("?");
        if ($flag >= 0) {
            $src = $src.substr(0, $flag);
        }
        obj.attr("src", $src + "?" + reload);
    },
    //验证输入的验证码
    verifyCaptchaId: function () {
        // $("#captchaCode").blur(function () {
        $.ajax({
            type: "GET",
            url: "/verifyCaptcha.py/" + $("#captchaCode").val() + "/" + $("#captchaId").val(),
            success: function (res) {
                if (res.code === -9) {
                    layer.msg(res.msg, {icon: 2, time: 2000, shade: 0.3}, function () {
                        login.flushCaptcha();
                        // $("#bn_login").attr({"disabled": "disabled"});
                    });
                } else {
                    // $("#bn_login").removeAttr("disabled");
                    console.info(1);
                    $("#verify_flag").val("1");
                }
            }
        });
    },
    //自动刷新验证码
    flushCaptcha: function () {
        $.ajax({
            type: "GET",
            url: "/flushCaptcha.py/",
            success: function (res) {
                $("#rcCaptcha-img").attr("src", "/captcha/" + res.data + ".png");
                $("#captchaId").val(res.data);
            }
        });
    },
    loginout: function () {
        var msg = "确认要离开了吗? 欢迎再次使用哟! ";
        layer.confirm(msg, {
            btn: ['确认', '取消'] //按钮
        }, function () {
            window.location.href = "/loginOut.py";
        });
    }
};

/*分页*/
//分页插件
var page_limit = 20,
    page_curr = 1,
    page_counts = 0;
var Page_Joker = {
    getOneData: function (query) {
        //请求数据
        var it;
        if (query === 'query') {
            $("#page").val(1);
        }
        $.ajax({
            type: 'GET',
            url: "/index/list_page.py/",
            data: $("form").serialize(),
            dataType: 'JSON',
            beforeSend: function () {
                it = X_loadLayer.x_show_layer("客官别急，马上执行您的操作...");
            },
            success: function (data) {
                X_loadLayer.x_close_layer(it);
                Page_Joker.showData(data.root);
                if (query === 'query') {
                    page_curr = 1;
                } else {
                    page_curr = data.page;
                }
                page_counts = data.count;//总页数
                page_limit = data.limit;//显示条数

                if (page_curr == 1) {
                    Page_Joker.toPage();
                }
            },
            error: function (XMLHttpRequest) {
                X_loadLayer.x_close_layer(it);
                layer.msg("登录信息过期,请刷新后,重新登录! " + XMLHttpRequest.status, {
                    icon: 2,
                    shade: 0.3,
                    time: 2000
                });
            }
        });
        return false;
    },
    showData: function (result) {
        //数据渲染,处理服务器返回的数据
        var con = "";
        $.each(result, function (index, item) {
            con += "<tr><td><div align='center'>" + (index + 1) + "</div></td>";
            con += "<td><div align='center'>" + item.TransOut + "</div></td>";
            con += "<td><div align='center'>" + item.TransIn + "</div></td>";
            con += "<td><div align='center'>" + item.AppId + "</div></td>";
            con += "<td><div align='center'>" + item.EditTime + "</div></td>";

            con += "<td class='td-manage'><div align='center'>";

            con += "<a title='修改' onclick=\"operator.modofy('" + item.Id + "')\" href='javascript:;'><i class='layui-icon' > &#xe642; </i></a>";
            //con += "<a title='修改' href='/index/modify_ui.py/" + item.Id + " '><i class='layui-icon' > &#xe642; </i></a>";
            con += "<a title='删除' onclick=\"operator.del_config('" + item.Id + "')\" href='javascript:;'><i class='layui-icon'>&#xe640;</i></a>";
            //con += "<a title='删除'  href='/index/modify_ui.py/" + item.Id + " '><i class='layui-icon'>&#xe640;</i></a>";

            con += "</div></td></tr>";
        });
        if (result == null || result === "" || result.length < 1) {
            con += "<tr><td colspan='8'><div align='center'>没有此类信息</div></td></tr>";
        }
        $("#show_data").html(con); //把内容入到这个div中即完成

    },
    toPage: function () {
        //调用分页
        layui.use(['laypage'], function () {
            var laypage = layui.laypage;
            laypage.render({
                elem: 'pageJoekr',
                count: page_counts,
                limit: page_limit,
                curr: page_curr,
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                jump: function (obj, first) {
                    page_curr = obj.curr;
                    page_counts = obj.count;
                    page_limit = obj.limit;
                    $("#page").val(page_curr);
                    $("#limit").val(page_limit);
                    $("#count").val(page_counts);
                    if (!first) {
                        Page_Joker.getOneData();//一定要把翻页的ajax请求放到这里，不然会请求两次。
                    }
                }
            });
        });
    }
};
